<?php

include_once 'GeneralDAO.php';
include_once 'Karyawan.php';

class KaryawanDAO extends GeneralDAO {

    public function insert($karyawan) {
        try {
            $this->DBH->beginTransaction();
            $STH = $this->DBH->prepare("INSERT INTO tkaryawan(id,nama,alamat,telp,email) 
                VALUES(:id,:nama,:alamat,:telp,:email)");
            $STH->execute((array) $karyawan);
            // backup
            $id = $this->DBH->lastInsertId('id');
            $tgl = date('Y-m-d H:i:s', time());
            $this->DBH->exec("INSERT INTO tkaryawanversion(changed,id,nama,alamat,telp,email) 
                VALUES('$tgl',$id,'$karyawan->nama','$karyawan->alamat','$karyawan->telp','$karyawan->email')");
            $this->DBH->commit();
        } catch (PDOException $ex) {
            $this->DBH->rollBack();
            echo $ex->getMessage();
        }
    }

    public function getAll() {
        try {
            $STH = $this->DBH->prepare("SELECT id,nama,alamat,telp,email FROM tkaryawan");
            $STH->execute();
            $STH->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Karyawan');
            $krys = array();
            while ($kry = $STH->fetch()) {
                array_push($krys, $kry);
            }
            return $krys;
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

}

?>
